package db.migrations;

import static com.eroi.migrate.Define.autoincrement;
import static com.eroi.migrate.Define.column;
import static com.eroi.migrate.Define.defaultValue;
import static com.eroi.migrate.Define.length;
import static com.eroi.migrate.Define.notnull;
import static com.eroi.migrate.Define.primarykey;
import static com.eroi.migrate.Define.table;
import static com.eroi.migrate.Define.uniqueIndex;
import static com.eroi.migrate.Define.DataTypes.BOOLEAN;
import static com.eroi.migrate.Define.DataTypes.INTEGER;
import static com.eroi.migrate.Define.DataTypes.LONGVARCHAR;
import static com.eroi.migrate.Define.DataTypes.NUMERIC;
import static com.eroi.migrate.Define.DataTypes.TIMESTAMP;
import static com.eroi.migrate.Define.DataTypes.TINYINT;
import static com.eroi.migrate.Define.DataTypes.VARCHAR;
import static com.eroi.migrate.Execute.addIndex;
import static com.eroi.migrate.Execute.createTable;
import static com.eroi.migrate.Execute.dropIndex;
import static com.eroi.migrate.Execute.dropTable;

import org.apache.log4j.Logger;

import com.eroi.migrate.Define.Length;
import com.eroi.migrate.Define.PrimaryKey;
import com.eroi.migrate.Migration;
import com.eroi.migrate.mchema.Column;

/**
 * 
 * Person
 * @author gongfu
 *
 */
public class Migration_1 implements Migration{

	private static final Logger logger  = Logger.getLogger(Migration_1.class);
	
	private final String T_P_B = "person_basic";
	private final String T_P_O = "person_other";
	private final String T_P_A = "person_attent";
	
	public void up(){
		logger.info("Migration_1_person.up--------------------->");
		createTable(
				table(T_P_B,
						column("id", INTEGER, "", primarykey(), notnull(), autoincrement()),
						column("email", VARCHAR, "", length(320), notnull()),
						column("name", VARCHAR, "", length(40), notnull()),
						column("password", VARCHAR, "密码1", length(32), notnull()),
						column("salt", VARCHAR, "密码2(双重加密)", length(32), notnull()),
						column("photo", VARCHAR, "phone url", length(100)), // http://www.gongfuzg.com/upload/photos/40
						column("sex", TINYINT, "1:男 0:女", defaultValue(1)),
						column("motto", VARCHAR, "个性签名", length(100)),
						column("province", TINYINT, "省代码"),
						column("city", TINYINT, "市代码"),
						column("town", TINYINT, "区代码"),
						column("attented_num", INTEGER, "粉丝数", defaultValue(0)),
						column("attent_num", INTEGER, "关注数", defaultValue(0)),
						column("credits",  INTEGER, "积分", defaultValue(0)),
						column("sign_time", TIMESTAMP, "注册时间", notnull()),
						column("is_able", TINYINT, "是否启用", defaultValue(1)),
						column("mark_ad", VARCHAR, "后台备注", length(100))
				)
		);
		
		createTable(
				table(T_P_O, 
						column("id", INTEGER, "", primarykey(), notnull(), autoincrement()),
						column("pb_id", INTEGER, "person_basic_id", notnull()),
						column("phone", VARCHAR, "电话", length(20)),
						column("qq", VARCHAR, "QQ", length(20)),
						column("age", TINYINT, "年龄(代码?)"),
						column("job", TINYINT, "工作代码"),
						column("interest", VARCHAR, "感兴趣代码", length(30)),
						column("good_at", VARCHAR, "特长", length(30))
				)
		);
		
		createTable(
				table(T_P_A, 
						column("id", INTEGER, "", primarykey(), notnull(), autoincrement()),
						column("attent_id", INTEGER, "关注人", notnull()),
						column("attented_id", INTEGER, "被关注人", notnull()),
						column("attent_time", INTEGER, "关注时间", notnull())
					)
		);
	}
	
	public void down(){
		logger.info("Migration_1_person.down--------------------->");
		dropTable(T_P_B);
		dropTable(T_P_O);
		dropTable(T_P_A);
	}
}
